package com.yidu.BusinessParament.controller;

import com.yidu.BusinessParament.entity.Cash;
import com.yidu.BusinessParament.service.CashService;
import com.yidu.BusinessParament.util.IdUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 现金账户的控制类
 * @Version 1.0
 * @Author zsc
 * @Date 2022/12/8 15:02
 */
@RestController
public class CashController {
    @Resource
    private CashService cashService;
    @Resource
    IdUtil idUtil;
    /**
     * 查询现金账号表
     * @param page  当前页
     * @param limit 每页显示数
     * @param cash  现金账户对象
     * @return
     */
    //这只是从数据库查询的一个方法，跟跳转界面没关系
    @RequestMapping("/SelectCash")
    public Map SelectCash(int page, int limit, Cash cash) {
        //查询加分页
        List<Cash> cashList = cashService.SelectCash (page, limit, cash);
        //查行数
        int count = cashService.SelectCashCount (cash);
        System.out.println ("行数" + count);
        Map map = new HashMap ();
        map.put ("code", 0);
        map.put ("msg", "");
        map.put ("count", count);
        map.put ("data", cashList);
        return map;
    }

    /**
     * 新增现金账户数据
     * @param cash 现金账户对象
     * @return
     */
    @RequestMapping("/InsertCash")
    public int InsertCash(Cash cash) {
        //调用SelectMaxCashId拿到最大ID
        String cash2 = cashService.SelectMaxCashId ();
        //获得最大ID
        String id = idUtil.createID (cash2);
        Cash cash1 = new Cash (id, cash.getBankId (), cash.getAccountName (), cash.getBankName (), cash.getStockpileType ()
                , cash.getCardRetes (), cash.getBearingTime (), cash.getOpenTime (), cash.getEndTime (), cash.getRemark (),cash.getMoney(),cash.getCasMoney());
        int i = cashService.InsertCash (cash1);
        if (i > 0) {
            System.out.println ("添加成功" + i);
        } else {
            System.out.println ("添加失败" + i);
        }
        return i;
    }

    /**
     * 删除现金账户数据
     * @param cashId 现金账户Id
     * @return
     */
    @RequestMapping("/DeleteCash")
    public int DeleteCash(String cashId) {
        System.out.println ("进来了删除的方法" + cashId);
        int i = cashService.DeleteCash (cashId);
        if (i > 0) {
            System.out.println ("删除成功" + i);
        } else {
            System.out.println ("删除失败" + i);
        }
        return i;
    }

    /**
     * 修改现金账户数据
     * @param cash 现金账户对象
     * @return
     */
    @RequestMapping("/UpdateCash")
    public int UpdateCash(Cash cash){
        System.out.println("进来了修改的方法"+cash);
        int i = cashService.UpdateCash (cash);
        if(i>0){
            System.out.println("修改成功"+i);
        }else{
            System.out.println("修改失败"+i);
        }
        return i;
    }
}
